package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminAdminService;
import com.qf.service.AdminBrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/brand")
@CrossOrigin
@RestController
public class AdminBrandController {

    @Autowired
    private AdminBrandService adminBrandService;

    @Autowired
    private AdminAdminService adminAdminService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 品牌制造商列表
     * @param page
     * @param limit
     * @param id
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌制造商列表")
    public Object brandList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            Integer id,String name,
                            String sort,String order){

        IPage<DtsBrand> iPage = adminBrandService.selectBrandList(page,limit,id,name,sort,order);

        long total = iPage.getTotal();
        List<DtsBrand> records = iPage.getRecords();

        DtsBrand[] items = new DtsBrand[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }


    @RequestMapping("/catAndAdmin")
    @RequiresPermissions("admin:brand:catAndAdmin")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌和管理员列表")
    public Object brandCatAndAdmin(){

        List<DtsAdmin> admin = adminAdminService.selectAll();

        HashMap[] adminList = new HashMap[admin.size()];

        for (int i = 0; i < admin.size(); i++) {
            HashMap<String, Object> adminResult = new HashMap<>();
            adminResult.put("value", admin.get(i).getId());
            adminResult.put("label", admin.get(i).getUsername());
            adminList[i] = adminResult;
        }


        //查询父类品牌
        List<DtsCategory> pCategories = categoryService.selectList(0);

        HashMap[] categoryList = new HashMap[pCategories.size()];

        for (int i = 0; i < pCategories.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("value", pCategories.get(i).getId());
            map.put("label", pCategories.get(i).getName());
            map.put("children", childCategoryList(pCategories.get(i).getId()));
            categoryList[i] = map;
        }

        //查询品牌的子类
        HashMap<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList", categoryList);

        return ResponseUtil.ok(data);
    }

    //查询品牌的子类
    public HashMap[] childCategoryList(Integer pid){
        List<DtsCategory> categories = categoryService.selectList(pid);

        HashMap[] categoryList = new HashMap[categories.size()];

        for (int i = 0; i < categories.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("value", categories.get(i).getId());
            map.put("label", categories.get(i).getName());
            map.put("children", null);
            categoryList[i] = map;
        }

        return categoryList;

    }

    //品牌制造商添加
    @PostMapping("/create")
    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌制造商添加")
    public Object brandCreate(@RequestBody DtsBrand dtsBrand){
        DtsBrand data = adminBrandService.insertBrand(dtsBrand);
        return ResponseUtil.ok(data);
    }

    //品牌制造商修改
    @PostMapping("/update")
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌制造商修改")
    public Object brandUpdate(@RequestBody DtsBrand dtsBrand){
        adminBrandService.updateBrand(dtsBrand);
        return dtsBrand;
    }

    //品牌制造商删除  逻辑删除
    @PostMapping("/delete")
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌制造商删除")
    public Object brandDelete(@RequestBody DtsBrand dtsBrand){
        adminBrandService.deleteBrand(dtsBrand.getId());
        return ResponseUtil.ok();
    }

}
